// Go API 配置和请求方法
const goBaseUrl = 'https://zwc1.asxgt.com';//'http://127.0.0.1:8080' //'https://pppbbb.usxgt.com';

// Go API 请求方法
const goHttpRequest = (opts, data) => {
	let httpDefaultOpts = {
		url: goBaseUrl + opts.url,
		data: data,
		method: opts.method,
		header: opts.method == 'get' ? {
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'Content-Type': 'application/json; charset=UTF-8'
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res)
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};

// Go API 带token请求
const goHttpTokenRequest = (opts, data) => {
	let getToken = uni.getStorageSync("token") == undefined || uni.getStorageSync("token") == '' ? '' : JSON.parse(
		uni.getStorageSync("token"));
	let httpDefaultOpts = {
		url: goBaseUrl + opts.url,
		data: data,
		method: opts.method,
		header: opts.method == 'get' ? {
			"Authorization": "Bearer " + getToken,
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			"Authorization": "Bearer " + getToken,
			'Content-Type': "application/json; charset=UTF-8"
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res)
				if (res[1].data.msg == '未授权') {
					// #ifdef H5
					//删除cookies
					document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
					// #endif
					// 删除本地缓存
					uni.removeStorageSync('token');
					uni.removeStorageSync('userinfo');
					uni.showToast({
						content:'登录信息已过期，请重新登录！',
						icon:'none'
					})
				}
			}).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};

export default {
	goBaseUrl,
	goHttpRequest,
	goHttpTokenRequest
}
